var HOME = {
	is_sending: false,
	filterIsMouseOver: false,
	productIsMouseOver: false,
	menuIsMouseOver: false,
	cateId: -1,
	init : function() {
		 var menuActive = $('#page-menu a.a-menu-home');
		 menuActive.addClass(menuActive.attr('class')+'-active');
		$('#nivo-slider').nivoSlider({
			effect : "fade",
			slices : 15,
			boxCols : 8,
			boxRows : 4,
			animSpeed : 500,
			pauseTime : 4500,
			startSlide : 0,
			directionNav : false,
			controlNav : true,
			controlNavThumbs : false,
			pauseOnHover : false,
			manualAdvance : false
		});
		$('div#product > div.content-left > div.show-filter > div.content > div').each(function(){
			var obj = $(this);
			if(parseInt(obj.attr('data-total'))>14){
				obj.css('height','400px').mCustomScrollbar({
					scrollButtons:{
						enable:false
					},
					scrollInertia:150,
					advanced:{
						updateOnBrowserResize:true, 
						updateOnContentResize:true, 
						autoExpandHorizontalScroll:false 
					},
					callbacks:{
						onTotalScroll:function(){
							
						},
						onTotalScrollOffset:5
					}
				});
			}
		});
		HOME.loadProductDetail(1);
	},
	initProduct: function(){
		$('.product-menu span.reset').click(function(){
			$('div.show-filter').fadeOut('fast');
			$('div.show-filter ul li').each(function(){
				$(this).attr('class','');
			});
			$('div.show-filter input[type=hidden]').each(function(){
				$(this).val('');
			});
			$(this).hide();
			HOME.loadProduct(1);
			return false;
		});
		$('div.show-filter ul li').click(function(){
			var oInput = ($('input#'+$(this).attr('data-refer')));
			if($(this).attr('class')!='active'){
				$(this).attr('class','active');
				oInput.val(oInput.val()+$(this).attr('data-value')+',');
			}else{
				$(this).attr('class','');
				oInput.val(oInput.val().replace($(this).attr('data-value')+',',''));
			}
			HOME.checkShowReset();
			HOME.loadProduct(1);
		});

		$('span.fancy-show').mouseover(function(){
			HOME.menuIsMouseOver = true;
			
			$('div.show-filter .content > div').hide();
			$('#'+$(this).attr('data-id')).show();
			$('div.show-filter').css('margin-left',$(this).attr('data-left')+'px');
			$('div.show-filter').show();
		}).mouseleave(function(){
			HOME.menuIsMouseOver = false;
		});
		$('div.product-menu').mouseover(function(){
			HOME.productIsMouseOver = true;
		}).mouseleave(function(){
			HOME.productIsMouseOver = false;
			setTimeout(function(){
				if(!HOME.filterIsMouseOver && !HOME.menuIsMouseOver){
					$('div.show-filter').hide();
				}
			},100);
		});
		$('div.show-filter').mouseover(function(){
			HOME.filterIsMouseOver = true;
		}).mouseleave(function(){
			HOME.filterIsMouseOver = false;
			var obj = $(this);
			setTimeout(function(){
				if(!HOME.productIsMouseOver && !HOME.menuIsMouseOver){
					obj.hide();
				}
			},100);
		});
		HOME.loadProduct(1);
		
	},
	checkShowReset: function(){
		var check = false;
		$('div.show-filter ul li').each(function(){
			if($(this).attr('class')=='active'){
				check = true;
				return;
			}
		});
		if(check){
			$('.product-menu span.reset').show();
		}else{
			$('.product-menu span.reset').hide();
		}
		
	},
	loadProduct: function(page){
		MAIN.showAjaxLoader();
		$('div.show-filter').hide();
		//$('div.product-content').fadeOut('fast');
		$.ajax({
			type: "POST",
			url: '/faq/getFaq',
			data: {
				'cate_id': $('input#searchCates').val()+'-1',
				'page': page
			},
			cache: false,
			success: function(data){
				HOME.is_sending = false;
				$('div.product-content').html(data);
				//$('div.product-content').fadeIn('fast');
				MAIN.hideAjaxLoader();
			}
		});
	},
	loadProductDetail: function(page){
		$.ajax({
			type: "POST",
			url: '/loan/getProducts',
			data: {
				'page': page
			},
			cache: false,
			success: function(data){
				HOME.is_sending = false;
				$('#loan .loan-content').html(data);
			}
		});
	},
	initForm: function(){
		$('select#FaqsSend_location_city_id').change(function(){
			$('input#FaqsSend_location_district_id').val('-1');
			var obj = $(this);
			$.ajax({
				type: "POST",
				url: '/faq/getDistrict',
				data: {
					'cate_id': obj.val()
				},
				cache: false,
				success: function(data){
					data = $.parseJSON(data);
					if(data.status==1){
						$('select#selDistrict').html('<option value="-1">...loading...</option>');
						var str='<option value="-1" selected>---quận/huyện---</option>';
						if(data.data.length){
							for(var i=0; i<data.data.length; i++){
								str+='<option value="'+data.data[i].id+'">'+data.data[i].name+'</option>';
							}
						}
						$('select#selDistrict').html('');
						$('select#selDistrict').append(str);
					}else{
						$('select#selDistrict').html('<option value="-1">---quận/huyện---</option>');
					}
				}
			});
		});
		$('select#selDistrict').change(function(){
			$('input#FaqsSend_location_district_id').val($(this).val());
		});
	}
	
}